﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using VSA.Applications.VSA.WebService;

namespace VSA.Applications
{
    public partial class EntranceForm : Form
    {
        OpenFileDialog openFile = new OpenFileDialog();
        MainService service = WebServiceFactory.GetService();
        Dictionary<String, String> dicImportCompany = new Dictionary<String, String>();

        public EntranceForm()
        {
            InitializeComponent();
            openFile.FileOk += new CancelEventHandler(openFile_FileOk);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.Hide();
            InfoEditGuideForm editForm = new InfoEditGuideForm(this);
            editForm.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.Hide();
            SearchGuideForm searchForm = new SearchGuideForm(this);
            searchForm.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExitMainForm();
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Minimized;
                    HideMainForm();
                }
                else if (this.WindowState == FormWindowState.Minimized)
                {
                    ShowMainForm();
                }
            }
        }

        private void EntranceForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }
        }

        private void EntranceForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            HideMainForm();
        }

        private void ExitMainForm()
        {
            this.TopMost = false;
            if (MessageBox.Show("Are you sure exit program?", "Confirm to exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.notifyIconObject.Visible = false;
                this.Close();
                this.Dispose();
                System.Environment.Exit(0);
            }
        }

        private void HideMainForm()
        {
            this.Hide();
        }

        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void importDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFile.ShowDialog();
        }

        private void openFile_FileOk(object sender, CancelEventArgs e)
        {
            string strFullPathName = openFile.FileName;
            string connStr = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + strFullPathName + ";Extended Properties=Excel 12.0;";
            OleDbConnection conn = new OleDbConnection(connStr);

            try
            {
                string sql = "SELECT * FROM [CompanyInfo$]";
                conn.Open();
                DataSet dsCompany = new DataSet();
                OleDbDataAdapter daCompany = new OleDbDataAdapter(sql, conn);
                daCompany.Fill(dsCompany);

                for (int i = 0; i <= dsCompany.Tables[0].Rows.Count - 1; i++)
                {
                    Company company = new Company();
                    company.Street = dsCompany.Tables[0].Rows[i]["Street"].ToString();
                    company.City = dsCompany.Tables[0].Rows[i]["City"].ToString();
                    company.Country = dsCompany.Tables[0].Rows[i]["Country"].ToString();
                    company.Continent = dsCompany.Tables[0].Rows[i]["Continent"].ToString();
                    company.CompanyName = dsCompany.Tables[0].Rows[i]["CompanyName"].ToString();
                    company.Website = dsCompany.Tables[0].Rows[i]["Website"].ToString();
                    company.Comments = dsCompany.Tables[0].Rows[i]["Comments"].ToString();
                    company.Email = dsCompany.Tables[0].Rows[i]["Email"].ToString();
                    company.Historic = dsCompany.Tables[0].Rows[i]["Historic"].ToString();
                    company.Telephone1 = dsCompany.Tables[0].Rows[i]["Telephone1"].ToString();
                    company.TelePhone2 = dsCompany.Tables[0].Rows[i]["TelePhone2"].ToString();
                    company.PostCode = dsCompany.Tables[0].Rows[i]["PostCode"].ToString();
                    company.Activity = dsCompany.Tables[0].Rows[i]["Activity"].ToString();
                    company.LatestUpdate = DateTime.Now;
                    company.UpdateBy = OfficeHelper.GetName();
                    company.CreateBy = OfficeHelper.GetName();
                    company.CreateDate = DateTime.Now;

                    int companyId = service.AddCompany(company);

                    dicImportCompany.Add(company.CompanyName, companyId.ToString());
                }

                sql = "SELECT * FROM [ProjectInfo$]";

                DataSet dsProject = new DataSet();
                OleDbDataAdapter daProject = new OleDbDataAdapter(sql, conn);
                daProject.Fill(dsProject);

                for (int i = 0; i <= dsProject.Tables[0].Rows.Count - 1; i++)
                {
                    Project project = new Project();
                    project.ProjectName = dsProject.Tables[0].Rows[i]["ProjectName"].ToString();
                    project.Note = dsProject.Tables[0].Rows[i]["Note"].ToString();
                    project.CreateDate = DateTime.Now;

                    service.AddProject(project);
                }

                sql = "SELECT * FROM [PersonInfo$]";

                DataSet dsPerson = new DataSet();
                OleDbDataAdapter daPerson = new OleDbDataAdapter(sql, conn);
                daPerson.Fill(dsPerson);

                for (int i = 0; i <= dsPerson.Tables[0].Rows.Count - 1; i++)
                {
                    ContactPerson person = new ContactPerson();
                    person.FamilyName = dsPerson.Tables[0].Rows[i]["FamilyName"].ToString();
                    person.GivenName = dsPerson.Tables[0].Rows[i]["GivenName"].ToString();
                    person.Functions = dsPerson.Tables[0].Rows[i]["Functions"].ToString();
                    person.DirectTelephone = dsPerson.Tables[0].Rows[i]["DirectTelephone"].ToString();
                    person.DirectEmail = dsPerson.Tables[0].Rows[i]["DirectEmail"].ToString();
                    person.MobileTelephone = dsPerson.Tables[0].Rows[i]["MobileTelephone"].ToString();
                    person.Comments = dsPerson.Tables[0].Rows[i]["Comments"].ToString();
                    person.Department = dsPerson.Tables[0].Rows[i]["Department"].ToString();
                    person.LatestUpdate = DateTime.Now;
                    person.UpdateBy = OfficeHelper.GetName();
                    person.CreateBy = OfficeHelper.GetName();
                    person.CreateDate = DateTime.Now;

                    int personId = service.AddContactPerson(person);

                    CompanyPersonMapping mapping = new CompanyPersonMapping();
                    mapping.CompanyId = Convert.ToInt32(dicImportCompany[dsPerson.Tables[0].Rows[i]["CompanyName"].ToString()]);
                    mapping.PersonId = personId;
                    mapping.CreateDate = DateTime.Now;
                    service.AddCompanyPersonMapping(mapping);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
